<?php
class BidagentsController extends AppController {

	var $name = 'Bidagents';
	var $helpers = array (
		'Html',
		'Form'
	);
	var $uses = array (
		'Bidagent',
		'Sale',
		'Licitation'
	);

	function placeBids() {
		
		$time = date('Y-m-d H:i:s', +strtotime("+5 seconds"));

		//Get All sales that time remaining = 5 seconds
		$sales = $this->Sale->find('all', array (
			'conditions' => array (
				'Sale.timeEnd',
				$time
			)
		));

		//If any sale was found
		if (!empty ($sales)) {
			foreach ($sales as $sale) {

				//Search for all the agents for this particular sale
				$agents = $this->Bidagent->find('all', array (
					'conditions' => array (
						'Sale.id',
						$sale['Sale']['id']
					)
				));

				if (!empty ($agents)) {
					$olderDateLicitation = date('Y-m-d  H:i:s');
					foreach ($agents as $agent) {

						//find the latest licitation for this agent in this sale
						$licitation = $this->Licitation->find('first', array (
							'conditions' => array (
								'Licitation.sale_id',
								$sale['Sale']['id'],
								'Licitation.user_id',
								$agent['User']['id']
							)
						));

						//If this licitation is the older one found, then save the agent and update olderDateLicitation
						if ($licitation['Licitation']['timeLicitation'] < $olderDateLicitation) {
							$olderDateLicitation = $licitation['Licitation']['timeLicitation'];
							$agentToPlaceBid = $agent;
						}
					}

					//Update Sale
					$price = $sale['Sale']['currentPrice'] + 0.01;
						$timeEnd = date('Y-m-d H:i:s', +strtotime("+15 seconds",strtotime($this->data['Sale']['timeEnd'])));
					$licitationTime = new DateTime();

					//Update Agent
					if ($sale->save(array (
							'user_id' => $agentToPlaceBid['Bidagent']['User']['id'],
							'currentPrice' => $price,
							'timeEnd' => $timeEnd,
							false
						)) && $licitation->save(array (
							'user_id' => $agentToPlaceBid['Bidagent']['User']['id'],
							'timeLicitation' => date('Y-m-d H:i:s', $licitationTime->format('U')),
							'price' => $price,
							'sale_id' => $sale['Sale']['id']
						)) && $agentToPlaceBid->saveField('licitationsLeft', $agentToPlaceBid['Bidagent']['licitationsLeft'] - 1)) {

						echo '---------'.date('Y-m-d H:i:s', $licitationTime->format('U')).'--------';
						echo 'Sale: ID:'.$sale['Sale']['id'].' article:'.$sale['Item']['name']. ' price '.$price;				
						echo 'User: '.$agentToPlaceBid['Bidagent']['User']['login'];
					}
				}

			}
		}

	}

	function index() {
		$this->Bidagent->recursive = 0;
		$this->set('bidagents', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Bidagent.', true));
			$this->redirect(array (
				'action' => 'index'
			));
		}
		$this->set('bidagent', $this->Bidagent->read(null, $id));
	}

	function add($sale_id = null) {

		if (!empty ($this->data)) {
			$user_id = $this->Auth->user('id');
			$licitations = $this->data['Sale']['licitationsLeft'];

			if (empty ($licitations) || $licitations < 0) {
				$this->Session->setFlash(__('Invalid number of licitations', true));
				$this->redirect(array (
					'controller' => 'sales',
					'action' => 'view',
					$sale_id
				));
			}

			App :: import('Model', 'User');
			$user = new User();
			$user->id = $this->Auth->user('id');
			$ourUser = $user->read(null, $this->Auth->user('id'));

			if ($licitations > $ourUser['User']['nrLicitations']) {
				$this->Session->setFlash(__("You don't have enought licitations avaiable", true));
				$this->redirect(array (
					'controller' => 'sales',
					'action' => 'view',
					$sale_id
				));
			}

			if ($this->Bidagent->save(array (
					'user_id' => $user_id,
					'sale_id' => $sale_id,
					'licitationsLeft' => $licitations
				)) && $user->saveField('nrLicitations', $ourUser['User']['nrLicitations'] - $licitations)) {
				$this->Session->write('Auth.User.nrLicitations', $ourUser['User']['nrLicitations'] - $licitations);
				$this->Session->setFlash(__('The Bidagent has been saved', true));
				$this->redirect(array (
					'controller' => 'sales',
					'action' => 'view',
					$sale_id
				));
			} else {
				$this->Session->setFlash(__('The Bidagent could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty ($this->data)) {
			$this->Session->setFlash(__('Invalid Bidagent', true));
			$this->redirect(array (
				'action' => 'index'
			));
		}
		if (!empty ($this->data)) {
			if ($this->Bidagent->save($this->data)) {
				$this->Session->setFlash(__('The Bidagent has been saved', true));
				$this->redirect(array (
					'action' => 'index'
				));
			} else {
				$this->Session->setFlash(__('The Bidagent could not be saved. Please, try again.', true));
			}
		}
		if (empty ($this->data)) {
			$this->data = $this->Bidagent->read(null, $id);
		}
		$sales = $this->Bidagent->Sale->find('list');
		$users = $this->Bidagent->User->find('list');
		$this->set(compact('sales', 'users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Bidagent', true));
			$this->redirect(array (
				'action' => 'index'
			));
		}
		if ($this->Bidagent->del($id)) {
			$this->Session->setFlash(__('Bidagent deleted', true));
			$this->redirect(array (
				'action' => 'index'
			));
		}
	}

}
?>